package com.pk.platform.business.charge.web;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import net.sf.json.JSONObject;

import com.pk.platform.business.charge.service.IChargeIndexService;
import com.pk.platform.business.charge.service.IChargeTempService;
import com.pk.platform.business.charge.service.ITuitionService;
import com.pk.platform.business.charge.vo.TuitionVO;
import com.pk.platform.business.common.service.IChildService;
import com.pk.platform.business.common.service.IClaService;
import com.pk.platform.business.common.service.IKindergartenService;
import com.pk.platform.business.core.web.GenericAction;
import com.pk.platform.business.data.service.IBaseDataService;
import com.pk.platform.domain.charge.ChargeIndex;
import com.pk.platform.domain.charge.ChargeTemp;
import com.pk.platform.domain.charge.TempIndexRef;
import com.pk.platform.domain.charge.Tuition;
import com.pk.platform.domain.common.Child;
import com.pk.platform.domain.common.Cla;
import com.pk.platform.domain.common.Kindergarten;
import com.pk.platform.domain.data.BaseData;
import com.pk.platform.domain.privilege.User;
import com.pk.platform.util.DateConverter;
import com.pk.platform.util.SelectOption;
import com.pk.platform.util.StringConverter;
import com.pk.platform.util.constant.ChargeConstant;
import com.pk.platform.util.constant.Constant;
import com.pk.platform.util.constant.PrivilegeConstant;

public class TuitionAction extends GenericAction {
	
	private ITuitionService tuitionService;
	
	private IClaService claService;
	
	private Tuition tuition = new Tuition();
	
	private TuitionVO tvo = new TuitionVO();
	
	private List<Cla> classList = new ArrayList<Cla>();
	
	private List<TempIndexRef> tirList = new ArrayList<TempIndexRef>();
	
	private IChargeTempService chargeTempService;
	
	private ChargeTemp ct = new ChargeTemp();
	
	private List<ChargeIndex> indexList = new ArrayList<ChargeIndex>();
	
	private IChargeIndexService chargeIndexService;
	
	private Child child = new Child();
	
	private IChildService childService;
	
	private String startYear; //开始年
	
	private String endYear; //结束年
	
	private String[] indexGroup; //指标组
	
	private String[] amountGroup; //金额组
	
	private String[] dateStartGroup; //费用开始日期

	private String[] dateEndGroup; //费用结束日期
	
	private String[] sortGroup; //排序组
	
	private IKindergartenService kindergartenService;
	
	private String tuitionId;
	
	private List<SelectOption> discountList = new ArrayList<SelectOption>(); //折扣列表
	
	private String discount; //折扣
	
	List<Map<String, Object>> titleBj = null;//打印标题(保教)
	
	List<Map<String, Object>> valueBj = null;//打印内容(保教)
	
	List<Map<String, Object>> title = null;//打印标题(非保教)
	
	List<Map<String, Object>> value = null;//打印内容(非保教)
	
	float subtotal1=0;
	
	float subtotal2=0;
	
	private User user = new User();
	
	private Kindergarten kinder = new Kindergarten();
	
	private String searchType;
	
	public String tuitionManage()
	{
		tvo.setKindergartenId(kindergartenId);
		tvo.getTuition().setIsDel(Constant.N_INT);
		classList = claService.queryClassList(kindergartenId);
		operateChn = PrivilegeConstant.getOperate(PrivilegeConstant.UPDATE_TUITION);
		if(ChargeConstant.SEARCH_TYPE_IMMEDIATE.equals(searchType))
		{
			lp = tuitionService.queryTuition(tvo, pager);
		}
		return SUCCESS;
	}
	
	public String toAddTuition()
	{
		user = this.getLoginUser();
		kinder = kindergartenService.queryKindergartenById(kindergartenId);
		classList = claService.queryClassList(kindergartenId);
		tirList = chargeTempService.queryTirList(ct.getId());
		indexList = chargeIndexService.queryChargeIndexList();
		child = childService.queryChildById(tuition.getChild().getId());
		ct = chargeTempService.queryChargeTempById(ct.getId());
		tuition.setCla(child.getCla());
		discountList = tuitionService.queryDiscountList();
		today = DateConverter.getToday();
		return SUCCESS;
	}
	
	public String toAddTuitionDetail()
	{
		tirList = chargeTempService.queryTirList(ct.getId());
		indexList = chargeIndexService.queryChargeIndexList();
		ct = chargeTempService.queryChargeTempById(ct.getId());
		discountList = tuitionService.queryDiscountList();
		today = DateConverter.getToday();
		return SUCCESS;
	}
	
	public String addTuition()
	{
		Kindergarten kinder = kindergartenService.queryKindergartenById(kindergartenId);
		tuition.setKindergarten(kinder);
		tuition.setRecordUser(this.getLoginUser());
		tuition.setRecordTime(new Date());
		tuition.setIsDel(Constant.N_INT);
		tuition.setIsPrint(Constant.N_INT);
		Map<String, String> returnObj = new HashMap<String, String>();
		try
		{
			tuitionService.addTuition(tuition, kinder, indexGroup,
					amountGroup, dateStartGroup, dateEndGroup,
					discount, ChargeConstant.ADD_TYPE_TEMP);
			tuitionId = tuition.getId();
			returnObj.put(Constant.RESULT, Constant.SUCCESS);
			returnObj.put("tuitionId", tuitionId);
		}
		catch (Exception e)
		{
			returnObj.put(Constant.RESULT, Constant.FAILURE);
			returnObj.put("message", e.getMessage());
		}
		message = "[" + JSONObject.fromObject(returnObj).toString() + "]";
		return SUCCESS;
	}
	
	public String toPrintTuition(){
		tuition = tuitionService.queryTuitionById(tuition.getId());
		titleBj = tuitionService.getTitle(tuition.getId(), 1);
		valueBj = tuitionService.getReportList(tuition.getId(), 1);
		title = tuitionService.getTitle(tuition.getId(), 0);
		value = tuitionService.getReportList(tuition.getId(), 0);
		subtotal1 = tuitionService.getsubTotal(tuition.getId(), 1);
		subtotal2 = tuitionService.getsubTotal(tuition.getId(), 0);
		return SUCCESS;
	}
	
	public String toUpdateTuition()
	{
		tuition = tuitionService.queryTuitionById(tuition.getId());
		return SUCCESS;
	}
	
	public String updateTuition()
	{
		tuitionService.updateTuition(tuition);
		return SUCCESS;
	}
	
	public String delTuition()
	{
		tuitionService.delTuition(tuition.getId());
		return SUCCESS;
	}
	
	public String addTuitionDetail()
	{
		tuition = tuitionService.queryTuitionById(tuition.getId());
		Map<String, String> returnObj = new HashMap<String, String>();
		try
		{
			tuitionService.superAddTuitionDetail(tuition, indexGroup, amountGroup,
					dateStartGroup, dateEndGroup, discount, ChargeConstant.ADD_TYPE_TEMP);
			returnObj.put(Constant.RESULT, Constant.SUCCESS);
		}
		catch (Exception e)
		{
			returnObj.put(Constant.RESULT, Constant.FAILURE);
			returnObj.put("message", e.getMessage());
		}
		message = "[" + JSONObject.fromObject(returnObj).toString() + "]";
		return SUCCESS;
	}
	
	public String toAddTuitionManual()
	{
		indexList = chargeIndexService.queryChargeIndexList();
		discountList = tuitionService.queryDiscountList();
		today = DateConverter.getToday();
		user = this.getLoginUser();
		kinder = kindergartenService.queryKindergartenById(kindergartenId);
		classList = claService.queryClassList(kindergartenId);
		tirList = chargeTempService.queryTirList(ct.getId());
		child = childService.queryChildById(tuition.getChild().getId());
		tuition.setCla(child.getCla());
		return SUCCESS;
	}
	
	public String addTuitionManual()
	{
		Kindergarten kinder = kindergartenService.queryKindergartenById(kindergartenId);
		tuition.setKindergarten(kinder);
		tuition.setRecordUser(this.getLoginUser());
		tuition.setRecordTime(new Date());
		tuition.setIsDel(Constant.N_INT);
		tuition.setIsPrint(Constant.N_INT);
		Map<String, String> returnObj = new HashMap<String, String>();
		try
		{
			tuitionService.addTuition(tuition, kinder, indexGroup,
					amountGroup, dateStartGroup, dateEndGroup,
					discount, ChargeConstant.ADD_TYPE_MANUAL);
			tuitionId = tuition.getId();
			returnObj.put(Constant.RESULT, Constant.SUCCESS);
			returnObj.put("tuitionId", tuitionId);
		}
		catch (Exception e) {
			returnObj.put(Constant.RESULT, Constant.FAILURE);
			returnObj.put("message", e.getMessage());
		}
		message = "[" + JSONObject.fromObject(returnObj).toString() + "]";
		return SUCCESS;
	}
	
	public String toAddTuitionDetailManual()
	{
		indexList = chargeIndexService.queryChargeIndexList();
		discountList = tuitionService.queryDiscountList();
		today = DateConverter.getToday();
		return SUCCESS;
	}
	
	public String addTuitionDetailManual()
	{
		tuition = tuitionService.queryTuitionById(tuition.getId());
		Map<String, String> returnObj = new HashMap<String, String>();
		try
		{
			tuitionService.superAddTuitionDetail(tuition, indexGroup, amountGroup,
					dateStartGroup, dateEndGroup, discount, ChargeConstant.ADD_TYPE_MANUAL);
			returnObj.put(Constant.RESULT, Constant.SUCCESS);
		}
		catch (Exception e) {
			returnObj.put(Constant.RESULT, Constant.FAILURE);
			returnObj.put("message", e.getMessage());
		}
		message = "[" + JSONObject.fromObject(returnObj).toString() + "]";
		return SUCCESS;
	}
	
	public String getIntTotal1()
	{
		String total = this.getSubtotal1() + "";
		String result = StringConverter.toNBitString(total, 0);
		return result;
	}
	
	public String getIntTotal2()
	{
		String total = this.getSubtotal2() + "";
		String result = StringConverter.toNBitString(total, 0);
		return result;
	}
	
	
	
	
	

	public ITuitionService getTuitionService() {
		return tuitionService;
	}

	public void setTuitionService(ITuitionService tuitionService) {
		this.tuitionService = tuitionService;
	}

	public IClaService getClaService() {
		return claService;
	}

	public void setClaService(IClaService claService) {
		this.claService = claService;
	}

	public Tuition getTuition() {
		return tuition;
	}

	public void setTuition(Tuition tuition) {
		this.tuition = tuition;
	}

	public TuitionVO getTvo() {
		return tvo;
	}

	public void setTvo(TuitionVO tvo) {
		this.tvo = tvo;
	}

	public List<Cla> getClassList() {
		return classList;
	}

	public void setClassList(List<Cla> classList) {
		this.classList = classList;
	}

	public List<TempIndexRef> getTirList() {
		return tirList;
	}

	public void setTirList(List<TempIndexRef> tirList) {
		this.tirList = tirList;
	}

	public IChargeTempService getChargeTempService() {
		return chargeTempService;
	}

	public void setChargeTempService(IChargeTempService chargeTempService) {
		this.chargeTempService = chargeTempService;
	}

	public ChargeTemp getCt() {
		return ct;
	}

	public void setCt(ChargeTemp ct) {
		this.ct = ct;
	}

	public List<ChargeIndex> getIndexList() {
		return indexList;
	}

	public void setIndexList(List<ChargeIndex> indexList) {
		this.indexList = indexList;
	}

	public IChargeIndexService getChargeIndexService() {
		return chargeIndexService;
	}

	public void setChargeIndexService(IChargeIndexService chargeIndexService) {
		this.chargeIndexService = chargeIndexService;
	}

	public Child getChild() {
		return child;
	}

	public void setChild(Child child) {
		this.child = child;
	}

	public IChildService getChildService() {
		return childService;
	}

	public void setChildService(IChildService childService) {
		this.childService = childService;
	}

	public String getStartYear() {
		return startYear;
	}

	public void setStartYear(String startYear) {
		this.startYear = startYear;
	}

	public String getEndYear() {
		return endYear;
	}

	public void setEndYear(String endYear) {
		this.endYear = endYear;
	}

	public String[] getIndexGroup() {
		return indexGroup;
	}

	public void setIndexGroup(String[] indexGroup) {
		this.indexGroup = indexGroup;
	}

	public String[] getAmountGroup() {
		return amountGroup;
	}

	public void setAmountGroup(String[] amountGroup) {
		this.amountGroup = amountGroup;
	}

	public String[] getDateStartGroup() {
		return dateStartGroup;
	}

	public void setDateStartGroup(String[] dateStartGroup) {
		this.dateStartGroup = dateStartGroup;
	}

	public String[] getDateEndGroup() {
		return dateEndGroup;
	}

	public void setDateEndGroup(String[] dateEndGroup) {
		this.dateEndGroup = dateEndGroup;
	}

	public String[] getSortGroup() {
		return sortGroup;
	}

	public void setSortGroup(String[] sortGroup) {
		this.sortGroup = sortGroup;
	}

	public IKindergartenService getKindergartenService() {
		return kindergartenService;
	}

	public void setKindergartenService(IKindergartenService kindergartenService) {
		this.kindergartenService = kindergartenService;
	}

	public String getTuitionId() {
		return tuitionId;
	}

	public void setTuitionId(String tuitionId) {
		this.tuitionId = tuitionId;
	}

	public List<SelectOption> getDiscountList() {
		return discountList;
	}

	public void setDiscountList(List<SelectOption> discountList) {
		this.discountList = discountList;
	}

	public String getDiscount() {
		return discount;
	}

	public void setDiscount(String discount) {
		this.discount = discount;
	}

	public List<Map<String, Object>> getTitleBj() {
		return titleBj;
	}

	public void setTitleBj(List<Map<String, Object>> titleBj) {
		this.titleBj = titleBj;
	}

	public List<Map<String, Object>> getValueBj() {
		return valueBj;
	}

	public void setValueBj(List<Map<String, Object>> valueBj) {
		this.valueBj = valueBj;
	}

	public List<Map<String, Object>> getTitle() {
		return title;
	}

	public void setTitle(List<Map<String, Object>> title) {
		this.title = title;
	}

	public List<Map<String, Object>> getValue() {
		return value;
	}

	public void setValue(List<Map<String, Object>> value) {
		this.value = value;
	}

	public float getSubtotal1() {
		return subtotal1;
	}

	public void setSubtotal1(float subtotal1) {
		this.subtotal1 = subtotal1;
	}

	public float getSubtotal2() {
		return subtotal2;
	}

	public void setSubtotal2(float subtotal2) {
		this.subtotal2 = subtotal2;
	}

	public User getUser() {
		return user;
	}

	public void setUser(User user) {
		this.user = user;
	}

	public Kindergarten getKinder() {
		return kinder;
	}

	public void setKinder(Kindergarten kinder) {
		this.kinder = kinder;
	}

	public String getSearchType() {
		return searchType;
	}

	public void setSearchType(String searchType) {
		this.searchType = searchType;
	}
	
}
